package com.hzw.service;

import com.hzw.api.ICommodityTypeService;
import com.hzw.common.DateUtils;
import com.hzw.dao.CommodityTypeDao;
import com.hzw.entity.CommodityType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class CommodityTypeService implements ICommodityTypeService {

    @Autowired
    CommodityTypeDao commodityTypeDao;

    @Override
    public List<CommodityType> selectAll(CommodityType commodityType) {
        return commodityTypeDao.select(commodityType);
    }

    @Override
    public CommodityType selectById(CommodityType commodityType) {
        return commodityTypeDao.selectById(commodityType);
    }

    @Override
    public void updateById(CommodityType commodityType) {
        commodityTypeDao.updateById(commodityType);

    }

    @Override
    public void delById(CommodityType commodityType) {
        commodityTypeDao.delById(commodityType);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insert(CommodityType commodityType) {
        List<CommodityType> resList = commodityTypeDao.select(commodityType);
        int res = 0;
        if (CollectionUtils.isEmpty(resList)) {
            commodityType.setId(UUID.randomUUID().toString().replace("-", ""));
            commodityType.setCreateTime(DateUtils.getNowTime());
            res = commodityTypeDao.insert(commodityType);
        }
        return res;
    }


}
